package lt.marsrutai.mobile.android.fragment;

import java.util.List;

import lt.marsrutai.mobile.android.AppContext;
import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.activity.intent.StopsIntentHolder;
import lt.marsrutai.mobile.android.activity.intent.TracksIntentHolder;
import lt.marsrutai.mobile.android.adapter.TrackAdapter;
import lt.marsrutai.mobile.android.exception.DataAccessException;
import lt.marsrutai.mobile.android.manager.ActivityManager;
import lt.marsrutai.mobile.android.model.Track;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockListFragment;


public class TracksFragment extends SherlockListFragment {

    private ActivityManager manager;

    private List<Track> tracks;
    private TrackAdapter adapter;
    private Runnable downloadTask;

    private String scopeFolder;
    private String scopeName;
    private String transportId;
    private String routeNumber;
    private String scheduleTrackName;
    private String scheduleId;
    private String[] trackIds;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.list_fragment, null);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        manager = new ActivityManager(getActivity());
        manager.showDownloadLoadingBar();

        receiveItemData();

        downloadData();
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        int resolvedPosition = adapter.resolveItemPosition(position);
        if (resolvedPosition >= 0) {
            sendItemData(resolvedPosition);
        }
    }

    public void receiveItemData() {
        TracksIntentHolder intentHolder = new TracksIntentHolder(getArguments());
        scopeFolder = intentHolder.getScopeFolder();
        scopeName = intentHolder.getScopeName();
        transportId = intentHolder.getTransportId();
        scheduleId = intentHolder.getScheduleId();
        routeNumber = intentHolder.getScheduleName();
        scheduleTrackName = intentHolder.getScheduleTrackName();
        trackIds = intentHolder.getTrackIds();
    }

    public void sendItemData(int i) {
        Track track = tracks.get(i);
        StopsIntentHolder intentHolder = new StopsIntentHolder(getActivity(),
            scopeFolder,
            scopeName,
            transportId,
            scheduleId,
            routeNumber,
            scheduleTrackName,
            track.getId(),
            track.getName()
        );
        startActivity(intentHolder.getIntent());
    }

    public void downloadData() {
        downloadTask = new Runnable() {

            @Override
            public void run() {
                getTracks(scopeFolder, scheduleId);
            }
        };
        Thread thread = new Thread(downloadTask);
        thread.start();
    }

    private void getTracks(String scopeName, String scheduleId) {
        try {
            if (trackIds == null) {
                tracks = AppContext.getDataService().getTracks(scopeName, scheduleId);

            }
            else {
                tracks = AppContext.getDataService().getTracksWithIds(scopeName, trackIds);
            }
        }
        catch (DataAccessException e) {
            manager.runOnUiThread(manager.errorRes);
            return;
        }
        manager.runOnUiThread(returnRes);
    }

    private Runnable returnRes = new Runnable() {

        @Override
        public void run() {

            adapter = new TrackAdapter(getActivity(), scopeFolder, tracks);
            setListAdapter(adapter);

            manager.dismissDownloadLoadingBar();
        }
    };

}